package org.example.test.service.impl;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.example.test.dal.entity.Favorites;
import org.example.test.dal.entity.Product;
import org.example.test.dal.mapper.FavoritesMapper;
import org.example.test.dal.mapper.ProductMapper;
import org.example.test.service.FavoritesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Component
public class FavoritesServiceImpl implements FavoritesService {
    private static final Logger LOGGER = LogManager.getLogger(FavoritesServiceImpl.class);

    @Autowired
    private FavoritesMapper favoritesMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public void addFavorites(Long productId, Long userId) {
        Boolean status = Boolean.TRUE;
        Favorites favorites = new Favorites();
        favorites.setProductId(productId);
        favorites.setUserId(userId);
        favorites.setStatus(status);
        favoritesMapper.insert(favorites);
    }

    @Override
    public void deleteByUserId(Long userId) {
        favoritesMapper.deleteByUserId(userId);
    }

    @Override
    public List<Product> select(Long userId) {
        List<Favorites> favoritesList = favoritesMapper.selectByUserId(userId);
        List<Product> productList = new ArrayList<>();
        for (Favorites favorites : favoritesList) {
            productList.add(productMapper.selectByPrimaryKey(favorites.getProductId()));
        }
        return productList;
    }

}
